package com.example.lovewordsapplication.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.example.lovewordsapplication.MyApplication;
import com.example.lovewordsapplication.R;
import com.example.lovewordsapplication.base.BaseActivity;
import com.example.lovewordsapplication.entity.UserInfo;
import com.example.lovewordsapplication.utils.ErrorCodeUtil;
import com.example.lovewordsapplication.utils.ToastUtil;

import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

/**
 * 登录账号
 *
 * @author Leung
 * @date 2020/4/7 23:37
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText etUsername;
    private EditText etPassword;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_login;
    }

    /**
     * 初始化控件
     */
    @Override
    protected void initView() {
        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
        // 登录
        Button btnLogin = (Button) findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);
        // 忘记密码
        TextView tvForgetPassword = findViewById(R.id.tv_forgetPassword);
        tvForgetPassword.setOnClickListener(this);
        // 注册
        TextView tvRegister = (TextView) findViewById(R.id.tv_register);
        tvRegister.setOnClickListener(this);
    }

    @Override
    protected String setTitle() {
        return "登录";
    }

    @Override
    protected boolean isShowBack() {
        return true;
    }

    /**
     * 提交登录
     */
    private void submit() {
        // validate
        String username = etUsername.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            toast("用户名不能为空");
            return;
        }
        String password = etPassword.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            toast("密码不能为空");
            return;
        }

        // 登录
        UserInfo userInfo = new UserInfo();
        userInfo.setUsername(username);
        userInfo.setPassword(password);
        userInfo.login(new SaveListener<UserInfo>() {
            @Override
            public void done(UserInfo userInfo, BmobException e) {
                // 判断是否成功登录
                if (e == null) {
                    toast("登录成功");
                    // 返回信息给MainActivity
                    Intent intent = new Intent();
                    intent.putExtra("username", userInfo.getUsername());
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    ErrorCodeUtil.getErrorMessage(e.getErrorCode(), MyApplication.getInstance().getContext());
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.btn_login:
                submit();
                break;
            case R.id.tv_register:
                intent.setClass(this, RegisterActivity.class);
                startActivityForResult(intent, MyApplication.REGISTER_REQUEST_CODE);
                break;
            case R.id.tv_forgetPassword:
                intent.setClass(this, ForgetActivity.class);
                startActivityForResult(intent, MyApplication.FORGET_PASSWORD_REQUEST_CODE);
                break;
            default:
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            String username = data.getStringExtra("username");
            etUsername.setText(username);
        }
    }

}
